package com.shop.shoporder.service.impl;

import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.entity.order.*;
import com.shop.shoporder.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("orderListService")
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    CartService cartService;
    @Autowired
    OrderAddressService orderAddressService;
    @Autowired
    ConfigService configService;
    @Autowired
    OrderService orderService;

    @Override
    public List<CartList> selectOrderList(Map<String, String> map, HttpServletRequest request) throws Exception {
        Integer usernoId=0;
        Integer orderStatus=0;
        Integer res=0;
        Order order=new Order();
        List<CartList> orderList=new ArrayList<>();
        if(map!=null){
            usernoId= InterfaceVer.VerNumber(map.get("userNoId"));
            orderStatus= InterfaceVer.VerNumber(map.get("orderStatus"));
            order=new Order();
            order.setUsernoId(usernoId);
            order.setOrderStatus(orderStatus+"");
            orderList=cartService.selectOrderList(order);
            if(orderList!=null&&orderList.size()>0){
                return orderList;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    @Override
    public SureOrder getOrderDetails(Map<String, String> map, HttpServletRequest request) throws Exception {
        SureOrder sureOrder=new SureOrder();
        OrderAddress orderAddress=new OrderAddress();
        Config config=new Config();
        List<CartList> cartList=new ArrayList<>();
        int userNoId = 0;
        String orderNo="";
        List<FreightData> listGuiGe=new ArrayList<>();
        //List<String> cartIdsList=new ArrayList<>();
        Order order=new Order();
        if (map != null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            orderNo = InterfaceVer.VerStr(map.get("orderNo"));

            order=orderService.selectByOrderNoAsDetails(orderNo);
            if(order!=null){
                int addressId=order.getAddressId()==null?0:order.getAddressId();
                orderAddress=orderAddressService.selectByPrimaryKey(addressId);
                order.setAddressId(null);
            }
            if(orderAddress!=null) {
                orderAddress.setProvinceId(null);
                orderAddress.setCityId(null);
                orderAddress.setAreaId(null);
                orderAddress.setIsdefault(null);
            }

            config=configService.selectByPrimaryKey(1);
            Config config1=new Config();
            if(config!=null){
                config1.setFinacename2(config.getFinacename2());
                config1.setFinacename3(config.getFinacename3());
            }
            cartList=cartService.selectCartListByOrderNo(userNoId,orderNo);

            sureOrder.setCartListList(cartList);
            sureOrder.setConfig(config1);
            sureOrder.setOrderAddress(orderAddress);
            sureOrder.setOrder(order);
        }
        return sureOrder;
    }
}
